package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprService empExprService;

    /**
     * 分页查询
     *
     * @param param
     * @return PageResult<Emp>
     * @param分页查询参数
     */
    @Override
    public PageResult<Emp> getPage(EmpQueryParam param) {
        Page<Emp> pageR = new Page(param.getPage(), param.getPageSize());

        pageR.addOrder(OrderItem.asc("entry_date"));

       /* LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null && !param.getName().equals(""), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());

        pageR = page(pageR,queryWrapper);*/

        pageR = lambdaQuery().like(param.getName() != null && !param.getName().equals(""), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(pageR);

        List<Emp> records = pageR.getRecords();

        if (records.size() > 0){
            List<Long> collect = records.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());

            List<Dept> depts = deptService.listByIds(collect);

            records.forEach(emp -> {
                depts.forEach(dept -> {
                    if (emp.getDeptId()!= null && emp.getDeptId().equals(dept.getId())){
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }


        return new PageResult<>(pageR.getTotal(), records);
    }

    /**
     * 新增员工 和 员工经历
     *
     * @param emp
     */
    @Override
    @Transactional
    public void saveEmp(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());

        this.save(emp);

        if (emp.getExprList().size() > 0){

            List<EmpExpr> exprList = emp.getExprList();
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });

            empExprService.saveBatch(exprList);
        }

    }

    @Override
    public Emp getEmp(Integer id) {
        Emp byId = this.getById(id);

        LambdaQueryWrapper<EmpExpr> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.in(EmpExpr::getEmpId, id);
        List<EmpExpr> list = empExprService.list(querryWrapper);
        byId.setExprList(list);
        return byId;
    }

    @Override
    public void updateEmp(Emp emp) {

        this.updateById(emp);
        // 先删除所有经历记录
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        // 再添加经历
        if (emp.getExprList().size() > 0){

            List<EmpExpr> exprList = emp.getExprList();
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });

            empExprService.saveBatch(exprList);
        }

    }

    @Override
    @Transactional
    public void removeEmps(List<Integer> ids) {
        super.removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}

